﻿using System;
using DAL.Mongo.Interface;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using DAL.Mongo.Configuration;

namespace DAL.Configuration
{
    public class MongoTestConfigurationManager : MongoConfigurationManager
    {
        public MongoTestConfigurationManager(IConfiguration config)
            : base(config)
        { }

        ~MongoTestConfigurationManager()
        {
            _database.Drop();
            _server.Disconnect();
        }

        public override MongoCollection<TEntity> GetCollectionFrom<TEntity>()
        {
            string entityName = GetEntityName<TEntity>();
            if (!_database.CollectionExists(entityName))
            {
                _database.CreateCollection(entityName, _config.Options);
            }

            return base.GetCollectionFrom<TEntity>();
        }
    }
}
